package com.personendb.gui;

import java.io.IOException;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.personendb.R;
import com.personendb.fotos.PassbildSpeicher;

public class DetailsProfilFragment extends Fragment {

	private int index = -1;
	private EditText vorname;
	private EditText nachname;
	private EditText email;
	private EditText telefonnummer;

	public static DetailsProfilFragment newInstance(int index) {
		DetailsProfilFragment f = new DetailsProfilFragment();

		// Supply index input as an argument.
		Bundle args = new Bundle();
		args.putInt("index", index);
		f.setArguments(args);

		return f;
	}

	public int getShownIndex() {
		return index;
	}

	public void setIndex(int index) {

		this.index = index;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		
		if (getArguments() != null && getArguments().containsKey("index")) {
			container.removeAllViews();
			View view = inflater.inflate(R.layout.profil_bearbeiten, container,
					false);
		
			int indexPlusOne = getArguments().getInt("index") + 1;
			ImageView passbild = (ImageView) view.findViewById(R.id.img_passbild);
			try {
				passbild.setImageBitmap(BitmapFactory.decodeStream(PassbildSpeicher.ladeFoto(getActivity(), indexPlusOne)));
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			vorname = (EditText) view
					.findViewById(R.id.edittext_vorname);
			vorname.setText("Person " + indexPlusOne);
			vorname.setEnabled(false);

			nachname = (EditText) view
					.findViewById(R.id.edittext_nachname);
			nachname.setEnabled(false);

			email = (EditText) view.findViewById(R.id.edittext_email);
			email.setEnabled(false);

			telefonnummer = (EditText) view
					.findViewById(R.id.edittext_telefonnummer);
			telefonnummer.setEnabled(false);

			Button buttonEdit = (Button) view.findViewById(R.id.button_edit);
			buttonEdit.setText(R.string.bearbeiten);
			buttonEdit.setOnClickListener(new OnClickListener() {

				public void onClick(View arg0) {
					Intent intent = new Intent(getActivity(),
							ProfilBearbeitenActivity.class);
					intent.putExtra(ProfilBearbeitenActivity.indexVorname, vorname.getText().toString());
					intent.putExtra(ProfilBearbeitenActivity.indexNachname, nachname.getText().toString());
					intent.putExtra(ProfilBearbeitenActivity.indexEmail, email.getText().toString());
					intent.putExtra(ProfilBearbeitenActivity.indexTelefonnummer, telefonnummer.getText().toString());
					
					startActivity(intent);
				}
			});

			return view;
		}

		return inflater.inflate(R.layout.start_logo, container,
				false);

	}
}
